package org.xyq.singleton.lazy;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class Main {
    private static final int THREAD_COUNT = 100;
    private static final ExecutorService executorService = Executors.newFixedThreadPool(THREAD_COUNT);

    /**
     * 测试多线程下的线程安全 ThreadSafeSynchronized
     *
     * @param args 命令行参数
     */
    public static void main(String[] args) {
        for (int i = 0; i < THREAD_COUNT; i++) {
            executorService.submit(() -> {
                try {
                    Thread.sleep(200);
                    ThreadSafeSynchronized instance = ThreadSafeSynchronized.getInstance();
                    System.out.println(instance);
                } catch (InterruptedException e) {
                    // 使用日志框架记录异常信息
                    System.err.println("Thread was interrupted: " + e.getMessage());
                    // 可选：重新抛出异常以保持中断状态
                    Thread.currentThread().interrupt();
                }
            });
        }

        // 关闭线程池并等待所有任务完成
        executorService.shutdown();
        try {
            if (!executorService.awaitTermination(60, TimeUnit.SECONDS)) {
                executorService.shutdownNow();
            }
        } catch (InterruptedException e) {
            executorService.shutdownNow();
        }
    }
}
